#include "stdafx.h"
#include "LuaPlayer.h"
#include <lua.h>

LuaPlayer::LuaPlayer()
{
}
LuaPlayer::~LuaPlayer()
{

}
void LuaPlayer::Register()
{
	//singleton<ScriptManager>::instance().RegisterPlayer("clearScript",LuaPlayer::luaClearScript);
	singleton<ScriptManager>::instance().RegisterPlayer("getTime",LuaPlayer::luaGetTime);
	singleton<ScriptManager>::instance().RegisterPlayer("LOGGER_ERROR",LuaPlayer::luaLOGGER_ERROR);
	singleton<ScriptManager>::instance().RegisterPlayer("LOGGER_INFO",LuaPlayer::luaLOGGER_INFO);
	singleton<ScriptManager>::instance().RegisterPlayer("getOnlineCount",LuaPlayer::luaGetOnlineCount);
	singleton<ScriptManager>::instance().RegisterPlayer("hasItem",LuaPlayer::luaHasItem);
	singleton<ScriptManager>::instance().RegisterPlayer("addItem",LuaPlayer::luaAddItem);
	singleton<ScriptManager>::instance().RegisterPlayer("addItemCount",LuaPlayer::luaAddItemCount);
	singleton<ScriptManager>::instance().RegisterPlayer("hasSkill",LuaPlayer::luaHasSkill);
	singleton<ScriptManager>::instance().RegisterPlayer("addSkill",LuaPlayer::luaAddSkill);
	singleton<ScriptManager>::instance().RegisterPlayer("hasPet",LuaPlayer::luaHasPet);
	singleton<ScriptManager>::instance().RegisterPlayer("addPet",LuaPlayer::luaAddPet);
	singleton<ScriptManager>::instance().RegisterPlayer("getRace",LuaPlayer::luaGetRace);
	singleton<ScriptManager>::instance().RegisterPlayer("getGender",LuaPlayer::luaGetGender);
	singleton<ScriptManager>::instance().RegisterPlayer("getLevel",LuaPlayer::luaGetLevel);
	singleton<ScriptManager>::instance().RegisterPlayer("getCharName",LuaPlayer::luaGetCharName);
	singleton<ScriptManager>::instance().RegisterPlayer("getCharId",LuaPlayer::luaGetCharId);
	
	singleton<ScriptManager>::instance().RegisterPlayer("addStamina",LuaPlayer::luaAddStamina);
	singleton<ScriptManager>::instance().RegisterPlayer("addEXP",LuaPlayer::luaAddEXP);
	singleton<ScriptManager>::instance().RegisterPlayer("addIngot",LuaPlayer::luaAddIngot);
	singleton<ScriptManager>::instance().RegisterPlayer("addMoney",LuaPlayer::luaAddMoney);
	singleton<ScriptManager>::instance().RegisterPlayer("addHonour",LuaPlayer::luaAddHonour);
	singleton<ScriptManager>::instance().RegisterPlayer("addWood",LuaPlayer::luaAddWood);
	singleton<ScriptManager>::instance().RegisterPlayer("addFood",LuaPlayer::luaAddFood);

	singleton<ScriptManager>::instance().RegisterPlayer("setNPCDialogAsk",LuaPlayer::luaSetNPCDialogAsk);
	singleton<ScriptManager>::instance().RegisterPlayer("setNPCDialogAnswer",LuaPlayer::luaSetNPCDialogAnswer);
	singleton<ScriptManager>::instance().RegisterPlayer("setNPCDialogAcceptIndex",LuaPlayer::luaSetNPCDialogAcceptIndex);
	singleton<ScriptManager>::instance().RegisterPlayer("setNPCDialogCompleteIndex",LuaPlayer::luaSetNPCDialogCompleteIndex);
	singleton<ScriptManager>::instance().RegisterPlayer("sendNPCDialog",LuaPlayer::luaSendNPCDialog);
	singleton<ScriptManager>::instance().RegisterPlayer("sendTalk",LuaPlayer::luaSendTalk);
	singleton<ScriptManager>::instance().RegisterPlayer("sendSubtitle",LuaPlayer::luaSendSubtitle);

	//singleton<ScriptManager>::instance().RegisterPlayer("setTaskNPCDialogAsk",LuaPlayer::luaSetNPCDialogAsk);
	//singleton<ScriptManager>::instance().RegisterPlayer("setTaskNPCDialogAnswer",LuaPlayer::luaSetNPCDialogAnswer);
	//singleton<ScriptManager>::instance().RegisterPlayer("setTaskNPCDialogAcceptIndex",LuaPlayer::luaSetNPCDialogAcceptIndex);
	//singleton<ScriptManager>::instance().RegisterPlayer("setTaskNPCDialogCompleteIndex",LuaPlayer::luaSetNPCDialogCompleteIndex);

	singleton<ScriptManager>::instance().RegisterPlayer("sendTaskNPCDialog",LuaPlayer::luaSendTaskNPCDialog);
	singleton<ScriptManager>::instance().RegisterPlayer("sendTaskTalk",LuaPlayer::luaSendTaskTalk);
	singleton<ScriptManager>::instance().RegisterPlayer("sendTaskSubtitle",LuaPlayer::luaSendTaskSubtitle);
	
	singleton<ScriptManager>::instance().RegisterPlayer("canAddTask",LuaPlayer::luaCanAddTask);
	singleton<ScriptManager>::instance().RegisterPlayer("addTask",LuaPlayer::luaAddTask);
	singleton<ScriptManager>::instance().RegisterPlayer("hasTask",LuaPlayer::luaHasTask);
	singleton<ScriptManager>::instance().RegisterPlayer("removeTask",LuaPlayer::luaRemoveTask);
	singleton<ScriptManager>::instance().RegisterPlayer("cancelTask",LuaPlayer::luaCancelTask);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskName",LuaPlayer::luaSetTaskName);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskDesc",LuaPlayer::luaSetTaskDesc);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskGoal",LuaPlayer::luaSetTaskGoal);
	singleton<ScriptManager>::instance().RegisterPlayer("clearTaskGoals",LuaPlayer::luaClearTaskGoals);
	singleton<ScriptManager>::instance().RegisterPlayer("sendTask",LuaPlayer::luaSendTask);
	singleton<ScriptManager>::instance().RegisterPlayer("getTaskData",LuaPlayer::luaGetTaskData);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskData",LuaPlayer::luaSetTaskData);
	singleton<ScriptManager>::instance().RegisterPlayer("getTaskStep",LuaPlayer::luaGetTaskStep);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskStep",LuaPlayer::luaSetTaskStep);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskStepSize",LuaPlayer::luaSetTaskStepSize);
	singleton<ScriptManager>::instance().RegisterPlayer("completeTask",LuaPlayer::luaCompleteTask);
	singleton<ScriptManager>::instance().RegisterPlayer("isTaskCompleted",LuaPlayer::luaIsTaskCompleted);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskCompleteNPC",LuaPlayer::luaSetTaskCompleteNPC);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskGoalCount",LuaPlayer::luaSetTaskGoalCount);
	singleton<ScriptManager>::instance().RegisterPlayer("setTaskGoalCountSize",LuaPlayer::luaSetTaskGoalCountSize);
	singleton<ScriptManager>::instance().RegisterPlayer("getTaskGoalCount",LuaPlayer::luaGetTaskGoalCount);
	singleton<ScriptManager>::instance().RegisterPlayer("getTaskGoalCountSize",LuaPlayer::luaGetTaskGoalCountSize);
	singleton<ScriptManager>::instance().RegisterPlayer("isTaskGoalCompleted",LuaPlayer::luaIsTaskGoalCompleted);
	singleton<ScriptManager>::instance().RegisterPlayer("resetTask",LuaPlayer::luaResetTask);

	singleton<ScriptManager>::instance().RegisterPlayer("jumpMap",LuaPlayer::luaJumpMap);
	singleton<ScriptManager>::instance().RegisterPlayer("enterBattlePVE",LuaPlayer::luaEnterBattlePVE);
	singleton<ScriptManager>::instance().RegisterPlayer("setBattleEndInfo",LuaPlayer::luaSetBattleEndInfo);
	singleton<ScriptManager>::instance().RegisterPlayer("sendSystemMessage",LuaPlayer::luaSendSystemMessage);
	singleton<ScriptManager>::instance().RegisterPlayer("sendInfo",LuaPlayer::luaSendInfo);
	singleton<ScriptManager>::instance().RegisterPlayer("addTrends",LuaPlayer::luaAddTrends);

	singleton<ScriptManager>::instance().RegisterPlayer("addTitle",LuaPlayer::luaAddTitle);
	singleton<ScriptManager>::instance().RegisterPlayer("hasTitle",LuaPlayer::luaHasTitle);
	singleton<ScriptManager>::instance().RegisterPlayer("removeTitle",LuaPlayer::luaRemoveTitle);

	singleton<ScriptManager>::instance().RegisterPlayer("addBuff",LuaPlayer::luaAddBuff);
	singleton<ScriptManager>::instance().RegisterPlayer("hasBuff",LuaPlayer::luaHasBuff);
	singleton<ScriptManager>::instance().RegisterPlayer("removeBuff",LuaPlayer::luaRemoveBuff);

	singleton<ScriptManager>::instance().RegisterPlayer("isInFaction",LuaPlayer::luaIsInFaction);
	singleton<ScriptManager>::instance().RegisterPlayer("startMining",LuaPlayer::luaStartMining);

	singleton<ScriptManager>::instance().RegisterPlayer("setImageId",LuaPlayer::luaSetImageId);
	singleton<ScriptManager>::instance().RegisterPlayer("addNPC",LuaPlayer::luaAddNPC);
	singleton<ScriptManager>::instance().RegisterPlayer("removeNPC",LuaPlayer::luaRemoveNPC);
}
Int32 LuaPlayer::luaGetTime(lua_State *L)
{
	lua_pushnumber(L, Int32(time(NULL)));
	return 1;
}
Player* LuaPlayer::GetPlayer(lua_State * L)
{
	lua_getglobal(L, "Player");
	if (lua_isnil(L,lua_gettop(L)))
		return NULL;
	return (Player*)(lua_topointer(L, lua_gettop(L)));
}
/*Int32 LuaPlayer::luaClearScript(lua_State *L)
{
	LOGGER_INFO("luaClearScript");
	singleton<ScriptManager>::instance().Clear();
	return 0;
}*/

Int32 LuaPlayer::luaLOGGER_ERROR(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	if (info.empty()) return 0;
	LOGGER_ERROR("%s",info.c_str());
	return 0;
}
Int32 LuaPlayer::luaLOGGER_INFO(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	if (info.empty()) return 0;
	LOGGER_INFO("%s",info.c_str());
	return 0;
}
Int32 LuaPlayer::luaGetOnlineCount(lua_State *L)
{
	Int32 nOnline = singleton<PlayerManager>::instance().GetOnlineCount();
	lua_pushnumber(L, nOnline);
	return 1;
}
Int32 LuaPlayer::luaHasPet(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaHasPet,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	//LOGGER_ERROR("luaHasPet,csv_id:%d",csv_id);
	lua_pushnumber(L, pPlayer->hasPet(csv_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaAddPet(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddPet,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	LOGGER_ERROR("luaAddPet,csv_id:%d",csv_id);
	pPlayer->addPet(csv_id);
	return 0;
}

Int32 LuaPlayer::luaHasItem(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaHasItem,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	//LOGGER_ERROR("luaHasItem,csv_id:%d",csv_id);
	lua_pushnumber(L, pPlayer->hasItem(csv_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaAddItem(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddItem,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	LOGGER_ERROR("luaAddItem,csv_id:%d",csv_id);
	pPlayer->addItem(csv_id);
	return 0;
}
Int32 LuaPlayer::luaAddItemCount(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -2);
	Int32 count = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaAddItemCount,csv_id:%d,count%d",csv_id,count);
	pPlayer->addItemCount(csv_id,count);
	return 0;
}
Int32 LuaPlayer::luaHasSkill(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaHasSkill,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	//LOGGER_ERROR("luaHasSkill,csv_id:%d",csv_id);
	lua_pushnumber(L, pPlayer->hasSkill(csv_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaAddSkill(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddSkill,Player == NULL,csv_id:%d",csv_id);
		return 0;
	}
	LOGGER_ERROR("luaAddSkill,csv_id:%d",csv_id);
	pPlayer->addSkill(csv_id);
	return 0;
}
Int32 LuaPlayer::luaGetRace(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->m_nRace);
	return 1;
}
Int32 LuaPlayer::luaGetGender(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->m_nGender);
	return 1;
}
Int32 LuaPlayer::luaGetLevel(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->m_nLevel);
	return 1;
}
Int32 LuaPlayer::luaGetCharName(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	if(pPlayer->m_strCharName.empty())
		return 0;
	lua_pushstring(L, pPlayer->m_strCharName.c_str());
	return 1;
}
Int32 LuaPlayer::luaGetCharId(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	if(pPlayer->m_strCharName.empty())
		return 0;
	lua_pushinteger(L, pPlayer->m_nCharId);
	return 1;
}
Int32 LuaPlayer::luaAddStamina(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddStamina,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddStamina,nValue:%d",nValue);
	lua_pushnumber(L, pPlayer->addStamina(nValue)?1:0);
	return 1;
}
Int32 LuaPlayer::luaAddEXP(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddEXP,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddEXP,nValue:%d",nValue);
	pPlayer->addEXP(nValue);
	return 0;
}
Int32 LuaPlayer::luaAddIngot(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddIngot,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddIngot,nValue:%d",nValue);
	pPlayer->addIngot(nValue);
	return 0;
}
Int32 LuaPlayer::luaAddMoney(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddMoney,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddMoney,nValue:%d",nValue);
	pPlayer->addMoney(nValue);
	return 0;
}
Int32 LuaPlayer::luaAddHonour(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddHonour,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddHonour,nValue:%d",nValue);
	pPlayer->addHonour(nValue);
	return 0;
}
Int32 LuaPlayer::luaAddWood(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddWood,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddWood,nValue:%d",nValue);
	pPlayer->addWood(nValue);
	return 0;
}
Int32 LuaPlayer::luaAddFood(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		LOGGER_ERROR("luaAddFood,Player == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_ERROR("luaAddFood,nValue:%d",nValue);
	pPlayer->addFood(nValue);
	return 0;
}
Int32 LuaPlayer::luaSetNPCDialogAsk(lua_State *L)
{
	const char* szRet = NULL;
	std::string strAsk;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strAsk = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->setNPCDialogAsk(strAsk);
	return 0;
}
Int32 LuaPlayer::luaSetNPCDialogAnswer(lua_State *L)
{
	const char* szRet = NULL;
	Int32 nIndex = lua_tointeger(L, -2);
	std::string strAnswer;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strAnswer = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->setNPCDialogAnswer(nIndex,strAnswer);
	return 0;
}
Int32 LuaPlayer::luaSetNPCDialogAcceptIndex(lua_State *L)
{
	Int32 nIndex = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->setNPCDialogAcceptIndex(nIndex);
	return 0;
}
Int32 LuaPlayer::luaSetNPCDialogCompleteIndex(lua_State *L)
{
	Int32 nIndex = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->setNPCDialogCompleteIndex(nIndex);
	return 0;
}
Int32 LuaPlayer::luaSendNPCDialog(lua_State *L)
{
	const char* szRet = NULL;
	std::string strCallback;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strCallback = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->sendNPCDialog(strCallback);
	return 0;
}
Int32 LuaPlayer::luaSendTalk(lua_State *L)
{
	//void sendTalk(Int32 type,Int32 npc_id,Int32 step,Int32 step_size,std::string strContent,std::string strCallback);
	const char* szRet = NULL;
	Int32 type = lua_tointeger(L, -7);
	Int32 image_id = lua_tointeger(L, -6);
	std::string strName;// = lua_tostring(L, -5);
	szRet = lua_tostring(L, -5);
	if(szRet != NULL) strName = szRet;
	Int32 step = lua_tointeger(L, -4);
	Int32 step_size = lua_tointeger(L, -3);
	std::string strContent;// = lua_tostring(L, -2);
	szRet = lua_tostring(L, -2);
	if(szRet != NULL) strContent = szRet;
	std::string strCallback;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strCallback = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->sendTalk(type,image_id,strName,step,step_size,strContent,strCallback);
	return 0;
}

Int32 LuaPlayer::luaSendSubtitle(lua_State *L)
{
	const char* szRet = NULL;
	std::string strContent;
	std::string strCallback;
	szRet = lua_tostring(L, -3);
	if(szRet != NULL)strContent = szRet;
	szRet = lua_tostring(L, -2);
	if(szRet != NULL)strCallback = szRet;
	Int32 index = lua_tointeger(L, -1);

	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->sendSubtitle(strContent,strCallback,index);
	return 0;
}
Int32 LuaPlayer::luaSendTaskNPCDialog(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	const char* szRet = NULL;
	std::string strCallback;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strCallback = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->m_nLastInteractiveTaskId = task_id;
	pPlayer->sendNPCDialog(strCallback);
	return 0;
}
Int32 LuaPlayer::luaSendTaskTalk(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -8);
	const char* szRet = NULL;
	Int32 type = lua_tointeger(L, -7);
	Int32 image_id = lua_tointeger(L, -6);
	std::string strName;// = lua_tostring(L, -5);
	szRet = lua_tostring(L, -5);
	if(szRet != NULL) strName = szRet;
	Int32 step = lua_tointeger(L, -4);
	Int32 step_size = lua_tointeger(L, -3);
	std::string strContent;// = lua_tostring(L, -2);
	szRet = lua_tostring(L, -2);
	if(szRet != NULL) strContent = szRet;
	std::string strCallback;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) strCallback = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->m_nLastInteractiveTaskId = task_id;
	pPlayer->sendTalk(type,image_id,strName,step,step_size,strContent,strCallback);
	return 0;
}
Int32 LuaPlayer::luaSendTaskSubtitle(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -4);
	const char* szRet = NULL;
	std::string strContent;
	std::string strCallback;
	szRet = lua_tostring(L, -3);
	if(szRet != NULL)strContent = szRet;
	szRet = lua_tostring(L, -2);
	if(szRet != NULL)strCallback = szRet;
	Int32 index = lua_tointeger(L, -1);

	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
		return 0;
	pPlayer->m_nLastInteractiveTaskId = task_id;
	pPlayer->sendSubtitle(strContent,strCallback,index);
	return 0;
}
Int32 LuaPlayer::luaCanAddTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->canAddTask(task_id)?1:0);
	return 1;
}

Int32 LuaPlayer::luaAddTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaAddTask,task_id:%d",task_id);
	pPlayer->addTask(task_id);
	return 0;
}
Int32 LuaPlayer::luaHasTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->hasTask(task_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaCancelTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->cancelTask(task_id);
	return 0;
}

Int32 LuaPlayer::luaRemoveTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->removeTask(task_id);
	return 0;
}
Int32 LuaPlayer::luaSetTaskName(lua_State *L)
{
	const char* szRet = NULL;
	std::string task_name;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL)task_name = szRet;
	Int32 task_id = lua_tointeger(L, -2);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskName(task_id,task_name);
	return 0;
}
Int32 LuaPlayer::luaSetTaskDesc(lua_State *L)
{
	const char* szRet = NULL;
	Int32 task_id = lua_tointeger(L, -2);
	std::string task_info;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL)task_info = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskDesc(task_id,task_info);
	return 0;
}
Int32 LuaPlayer::luaSetTaskGoal(lua_State *L)
{
	const char* szRet = NULL;
	Int32 nIndex = -8;
	Int32 task_id = lua_tointeger(L, nIndex++);
	Int32 goal_index = lua_tointeger(L, nIndex++);
	std::string goal_desc;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, nIndex++);
	if(szRet != NULL)goal_desc = szRet;
	Int32 goal_map_id = lua_tointeger(L, nIndex++);
	Int32 goal_npc_id = lua_tointeger(L, nIndex++);
	Int32 goal_position_map_id = lua_tointeger(L, nIndex++);
	Int32 goal_position_map_x = lua_tointeger(L, nIndex++);
	Int32 goal_position_map_y = lua_tointeger(L, nIndex++);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}

	pPlayer->setTaskGoal(task_id,goal_index,goal_desc,goal_map_id,goal_npc_id,goal_position_map_id,goal_position_map_x,goal_position_map_y);
	return 0;
}
Int32 LuaPlayer::luaClearTaskGoals(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->clearTaskGoals(task_id);
	return 0;
}
Int32 LuaPlayer::luaSendTask(lua_State *L)
{
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->PrepareSendTask();
	//pPlayer->sendTask();
	return 0;
}
Int32 LuaPlayer::luaGetTaskData(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 index = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->getTaskData(task_id,index));
	return 1;
}
Int32 LuaPlayer::luaSetTaskData(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -3);
	Int32 index = lua_tointeger(L, -2);
	Int32 val = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskData(task_id,index,val);
	return 0;
}
Int32 LuaPlayer::luaGetTaskStep(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->getTaskStep(task_id));
	return 1;
}
Int32 LuaPlayer::luaSetTaskStepSize(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 val = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskStepSize(task_id,val);
	return 0;
}
Int32 LuaPlayer::luaSetTaskStep(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 val = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskStep(task_id,val);
	return 0;
}
Int32 LuaPlayer::luaCompleteTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->completeTask(task_id);
	return 0;
}
Int32 LuaPlayer::luaIsTaskCompleted(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->isTaskCompleted(task_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaIsTaskGoalCompleted(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 goal_index = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->isTaskGoalCompleted(task_id,goal_index)?1:0);
	return 1;
}
Int32 LuaPlayer::luaSetTaskCompleteNPC(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -3);
	Int32 complete_map_id = lua_tointeger(L, -2);
	Int32 complete_npc_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskCompleteNPC(task_id,complete_map_id,complete_npc_id);
	return 0;
}
Int32 LuaPlayer::luaSetTaskGoalCount(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -3);
	Int32 goal_index = lua_tointeger(L, -2);
	Int32 goal_count = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskGoalCount(task_id,goal_index,goal_count);
	return 0;
}
Int32 LuaPlayer::luaSetTaskGoalCountSize(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -3);
	Int32 goal_index = lua_tointeger(L, -2);
	Int32 goal_count_size = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setTaskGoalCountSize(task_id,goal_index,goal_count_size);
	return 0;
}
Int32 LuaPlayer::luaGetTaskGoalCount(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 goal_index = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->getTaskGoalCount(task_id,goal_index));
	return 1;
}
Int32 LuaPlayer::luaGetTaskGoalCountSize(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 goal_index = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->getTaskGoalCountSize(task_id,goal_index));
	return 1;
}
Int32 LuaPlayer::luaResetTask(lua_State *L)
{
	Int32 task_id = lua_tointeger(L, -2);
	Int32 goal_index = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->m_stCharTaskInfo.m_nIdMainCompleted = 0;
	pPlayer->PrepareSendTask();
	return 0;
}
Int32 LuaPlayer::luaJumpMap(lua_State *L)
{
	Int32 nMapId = lua_tointeger(L, -3);
	Int32 nMapX = lua_tointeger(L, -2);
	Int32 nMapY = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	if(pPlayer->isInTeam() && !pPlayer->isTeamLeader())
	{
		LOGGER_ERROR("luaJumpMap:pPlayer->isInTeam() && !pPlayer->isTeamLeader()");
		return 0;
	}
	if(pPlayer->IsInBattle())
		return 0;
	Map* pMap = singleton<MapManager>::instance().Get(pPlayer->GetMapId());
	if (pMap == NULL)
	{
		LOGGER_ERROR("luaJumpMap:pMap == NULL");
		return 0;
	}
	Int32 nLineId = pPlayer->GetMapId().nLineId;
	if(pPlayer->GetMapId() == MapId(nMapId,nLineId))
	{
		pPlayer->PrepareMove(nMapX,nMapY,false);
		return 0;
	}
	else
	{
		pPlayer->PrepareJumpMap(MapId(nMapId,nLineId),nMapX,nMapY);
	}
	return 0;
}

Int32 LuaPlayer::luaEnterBattlePVE(lua_State *L)
{
	Int32 nLuaId = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	if(pPlayer->IsInBattle())
	{
		LOGGER_ERROR("luaEnterBattlePVE:pPlayer->IsInBattle()");
		return 0;
	}
	singleton<BattleManager>::instance().EnterBattlePVE(pPlayer->GetPlayerId(),nLuaId,Battle::BATTLE_TYPE_PVE);
	return 0;
}
Int32 LuaPlayer::luaSetBattleEndInfo(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL)info = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setBattleEndInfo(info);
	return 0;
}

Int32 LuaPlayer::luaSendSystemMessage(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL)info = szRet;
	//Player* pPlayer = GetPlayer(L);
	//if (pPlayer == NULL)
	//{
	//	return 0;
	//}
	if(!info.empty())
		singleton<GameServer>::instance().sendSystemMessage(info,false);
	return 0;
}
Int32 LuaPlayer::luaSendInfo(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;// = lua_tostring(L, -1);
	szRet = lua_tostring(L, -1);
	if(szRet != NULL)info = szRet;
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->sendInfo(info,0,false);
	return 0;
}

Int32 LuaPlayer::luaAddTrends(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;// = lua_tostring(L, -2);
	szRet = lua_tostring(L, -3);
	if(szRet != NULL)info = szRet;
	Int32 nType= lua_tointeger(L, -2);
	Int32 nAddToWorld= lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->addTrends(info,nType,nAddToWorld == 1);
	return 0;
}

Int32 LuaPlayer::luaAddTitle(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaAddTitle,csv_id:%d",csv_id);
	pPlayer->addTitle(csv_id);
	pPlayer->sendTitles();
	return 0;
}
Int32 LuaPlayer::luaHasTitle(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->hasTitle(csv_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaRemoveTitle(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaRemoveTitle,csv_id:%d",csv_id);
	pPlayer->removeTitle(csv_id);
	pPlayer->sendTitles();
	return 0;
}

Int32 LuaPlayer::luaAddBuff(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -2);
	Int32 retention_time = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaAddBuff,csv_id:%d",csv_id);
	pPlayer->addBuff(csv_id,retention_time);
	pPlayer->sendBuffs();
	return 0;
}
Int32 LuaPlayer::luaHasBuff(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->hasBuff(csv_id)?1:0);
	return 1;
}
Int32 LuaPlayer::luaRemoveBuff(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	LOGGER_ERROR("luaRemoveBuff,csv_id:%d",csv_id);
	pPlayer->removeBuff(csv_id);
	pPlayer->sendBuffs();
	return 0;
}

Int32 LuaPlayer::luaIsInFaction(lua_State *L)
{
	Int32 csv_id = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pPlayer->isInFaction()?1:0);
	return 1;
}
Int32 LuaPlayer::luaStartMining(lua_State *L)
{
	Int32 nPage = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->startMining(nPage,nIndex);
	return 0;
}

Int32 LuaPlayer::luaSetImageId(lua_State *L)
{
	Int32 nImageId = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	pPlayer->setImageId(nImageId);
	return 0;
}

Int32 LuaPlayer::luaAddNPC(lua_State *L)
{
	const char* szRet = NULL;
	Int32 nCSVId = lua_tointeger(L, -9);
	Int32 nOwnerCharId = lua_tointeger(L, -8);
	std::string strTitle;
	szRet = lua_tostring(L, -7);
	if(szRet != NULL)strTitle = szRet;
	std::string strName;
	szRet = lua_tostring(L, -6);
	if(szRet != NULL)strName = szRet;
	Int32 nImageId = lua_tointeger(L, -5);
	Int32 nRetentionTime = lua_tointeger(L, -4);
	Int32 nMapId = lua_tointeger(L, -3);
	Int32 nMapX = lua_tointeger(L, -2);
	Int32 nMapY = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	Map* pMap = singleton<MapManager>::instance().Get(pPlayer->GetMapId());
	if (pMap == NULL)
	{
		return 0;
	}
	if(nMapId == 0)
		nMapId = pPlayer->GetMapId().nMapId;

	if(nMapX == 0 || nMapY == 0)
	{
		nMapX = pPlayer->m_nMapX;
		nMapY = pPlayer->m_nMapY;
	}

	pMap->AddNPC(nCSVId,nOwnerCharId,strTitle,strName,nImageId,nRetentionTime,nMapId,nMapX,nMapY);
	return 0;
}
Int32 LuaPlayer::luaRemoveNPC(lua_State *L)
{
	Int32 nCSVId = lua_tointeger(L, -2);
	Int32 nOwnerCharId = lua_tointeger(L, -1);
	Player* pPlayer = GetPlayer(L);
	if (pPlayer == NULL)
	{
		return 0;
	}
	Map* pMap = singleton<MapManager>::instance().Get(pPlayer->GetMapId());
	if (pMap == NULL)
	{
		return 0;
	}
	pMap->RemoveNPC(nCSVId,nOwnerCharId);
	return 0;
}